﻿using System;
using System.Web.UI;
using Ninject.Core;
using Example.Presentation.Presenters;
using Example.Presentation.Kernel;

namespace Example.Presentation.Views
{
    /// <summary>
    /// Defines a base implementation for a Page View, defining the Presenter to be used and the View interface to be used.
    /// This base class is responsible for wiring up the presenter into the Page lifecycle
    /// </summary>
    public class BasePageView<TPresenter, TView> : Page
        where TView : IView
        where TPresenter : PresenterBase<TView>
    {
        public TPresenter Presenter { get; private set; }

        public BasePageView()
        {
            base.Init += (sender, e) =>
            {
                Presenter = (TPresenter)ServiceLocator.Resolve<TPresenter>();
                Presenter.View = (TView)sender;
            };

            base.Load += (sender, e) => Presenter.Load();

            base.Unload += (sender, e) =>
            {
                if (Presenter != null)
                    Presenter.Dispose();
            };
        }
    }
}
